/**
 * API设置功能测试程序
 */
public class APISettingsTest {
    
    public static void main(String[] args) {
        System.out.println("=== API设置功能验证 ===\n");
        
        try {
            // 1. 测试UserPreferences的API配置功能
            testUserPreferencesAPIConfig();
            
            // 2. 测试API配置面板
            testAPIConfigurationPanel();
            
            // 3. 测试WeatherApp的多源API管理
            testWeatherAppMultiAPI();
            
            System.out.println("\n🎉 所有API设置功能验证成功！");
            
        } catch (Exception e) {
            System.err.println("❌ 验证失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    private static void testUserPreferencesAPIConfig() {
        System.out.println("1. 测试UserPreferences API配置功能");
        
        UserPreferences prefs = new UserPreferences();
        
        // 测试API密钥设置
        prefs.setOpenWeatherMapApiKey("test_openweather_key_12345678901234567890");
        prefs.setWeatherApiComKey("test_weatherapi_key_12345678901234567890");
        
        System.out.println("   ✓ OpenWeatherMap密钥设置: " + 
            (prefs.hasValidOpenWeatherMapKey() ? "有效" : "无效"));
        System.out.println("   ✓ WeatherAPI.com密钥设置: " + 
            (prefs.hasValidWeatherApiComKey() ? "有效" : "无效"));
        
        // 测试API源配置
        prefs.setPrimaryApiSource("openweather");
        prefs.setBackupApiSource("weatherapi");
        prefs.setAggregationStrategy("PARALLEL_AGGREGATE");
        
        System.out.println("   ✓ 主要API源: " + prefs.getPrimaryApiSource());
        System.out.println("   ✓ 备用API源: " + prefs.getBackupApiSource());
        System.out.println("   ✓ 聚合策略: " + prefs.getAggregationStrategy());
        
        // 测试高级设置
        prefs.setApiTimeout(10000);
        prefs.setMaxRetries(5);
        prefs.setEnableApiFailover(true);
        
        System.out.println("   ✓ API超时: " + prefs.getApiTimeout() + "ms");
        System.out.println("   ✓ 最大重试: " + prefs.getMaxRetries());
        System.out.println("   ✓ 故障切换: " + prefs.isEnableApiFailover());
        System.out.println();
    }
    
    private static void testAPIConfigurationPanel() {
        System.out.println("2. 测试API配置面板");
        
        UserPreferences prefs = new UserPreferences();
        prefs.setOpenWeatherMapApiKey("test_key_1234567890123456789012345678");
        
        // 创建API配置面板
        APIConfigurationPanel panel = new APIConfigurationPanel(prefs);
        
        System.out.println("   ✓ API配置面板创建成功");
        System.out.println("   ✓ 面板大小: " + panel.getPreferredSize());
        System.out.println("   ✓ 面板组件数: " + panel.getComponentCount());
        
        // 测试MultiAPIManager连接
        MultiAPIManager manager = new MultiAPIManager(prefs);
        panel.setMultiAPIManager(manager);
        
        System.out.println("   ✓ MultiAPIManager连接成功");
        
        // 测试状态刷新
        panel.refreshServiceStatus();
        System.out.println("   ✓ 服务状态刷新完成");
        
        // 清理
        manager.shutdown();
        System.out.println();
    }
    
    private static void testWeatherAppMultiAPI() {
        System.out.println("3. 测试WeatherApp多源API管理");
        
        WeatherApp app = new WeatherApp();
        
        System.out.println("   ✓ WeatherApp初始化完成");
        
        // 获取MultiAPIManager
        MultiAPIManager manager = app.getMultiAPIManager();
        System.out.println("   ✓ MultiAPIManager获取成功");
        System.out.println("   ✓ 注册的API服务数: " + manager.getRegisteredServices().size());
        
        // 列出所有注册的API服务
        for (WeatherAPIService service : manager.getRegisteredServices()) {
            System.out.println("     - " + service.getDisplayName() + " (" + service.getSourceId() + 
                             ") 优先级: " + service.getPriority());
        }
        
        // 测试API重新初始化
        app.reinitializeAPIManager();
        System.out.println("   ✓ API管理器重新初始化成功");
        
        // 清理
        app.shutdown();
        System.out.println("   ✓ 应用程序清理完成");
        System.out.println();
    }
} 